// pages/my/my.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({
  /* ---------- 初始数据 ---------- */
  data: {
    historyNews: [] ,// 浏览记录

    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
  },
  // 推出登录
  handleLogout() {
    wx.showModal({
      title: '确认退出',
      content: '退出后将清除你的登录信息',
      success: (res) => {
        if (res.confirm) {
          // 1. 重置用户信息为默认状态
          this.setData({
            userInfo: {
              avatarUrl: defaultAvatarUrl, // 恢复默认头像
              nickName: '' // 清空昵称
            },
            hasUserInfo: false // 标记为未登录
          });

          // 2. 可选：清除本地存储的用户信息（如果之前有存的话）
          wx.removeStorageSync('userInfo'); // 若之前用本地存储保存过用户信息

          // 3. 提示退出成功
          wx.showToast({
            title: '已退出登录',
            icon: 'none',
            duration: 1500
          });
        }
      }
    });
  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const { avatarUrl } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },

  /* ---------- 生命周期 ---------- */
  onShow() {
    const history = wx.getStorageSync('historyNews') || [];
    console.log('【my页】读取浏览记录：', history);
    this.setData({ historyNews: history });
  },

  /* ---------- 跳转详情 ---------- */
  goDetail(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({ url: `../detail/detail?id=${id}` });
  },

  /* ---------- 单条删除 ---------- */
  deleteHistory(e) {
    const id = e.currentTarget.dataset.id;
    let history = wx.getStorageSync('historyNews') || [];
    history = history.filter(item => item.id !== id);
    wx.setStorageSync('historyNews', history);
    this.setData({ historyNews: history });
    wx.showToast({ title: '已删除', icon: 'none' });
  },

  /* ---------- 一键清空 ---------- */
  clearAll() {
    wx.showModal({
      title: '提示',
      content: '确定清空所有浏览记录？',
      success: res => {
        if (res.confirm) {
          wx.removeStorageSync('historyNews');
          this.setData({ historyNews: [] });
        }
      }
    });
  },

  /* ---------- 其余生命周期（空函数防报错） ---------- */
  onLoad() {},
  onReady() {},
  onHide() {},
  onUnload() {},
  onPullDownRefresh() {},
  onReachBottom() {},
  onShareAppMessage() {}
});